#!/bin/bash
# Copyright (C) 2019 Checkmk GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

# ------------------------------------------------------------------ #
#   Checkmk Caching Agent                                           #
#                                                                    #
#   This is a wrapper for the normal check_mk_agent. It optimizes    #
#   situations where - due to fully redundant monitoring - the       #
#   target host is queried by two or even more monitoring servers    #
#   in parallel. It will cache with output of the agent and send     #
#   the contents of the cache file to servers that have not seen     #
#   that output yet. No time criteria is needed, so this is working  #
#   without configuration, regardless of your check interval. It     #
#   is not even necessary that all servers are polling with the      #
#   same interval.                                                   #
#                                                                    #
#   We apoplogize for the fact that this wrapper is running only     #
#   on Linux, currently and uses features of the BASH (for sake      #
#   of execution speed). An adaption for other Unices should not     #
#   be very difficult.                                               #
#                                                                    #
#   In order to use this wrapper, simply call it instead of          #
#   check_mk_agent. This works via SSH and via xinetd. I'm not       #
#   sure if the normal inetd gives you access to the REMOTE_HOST     #
#   ip address, which is absolutely needed here.                     #
# ------------------------------------------------------------------ #

export MK_CACHEDIR="/var/cache/check_mk"

# Determine the IP address of the remote Nagios server.
# xinetd sends us the IP address of the remote host via
# the environment variable REMOTE_HOST. SSH sets a variable
# SSH_CONNECTION where the remote IP address is the first
# part of a space-separated list. Of both fails, we set
# the address to 'unknown'

if [ -z "$REMOTE_HOST" ]; then
    REMOTE_HOST=${SSH_CONNECTION%% *}
    if [ -z "$REMOTE_HOST" ]; then
        REMOTE_HOST=unknown
    fi
fi

# We keep the cache and state files in the configuration directory.
# This is not fully FHS-compliant - I guess - but (1) consistent
# with the logwatch extension and (2) configurable for the admin
# during setup.

mkdir -p $MK_CACHEDIR
CACHEFILE=$MK_CACHEDIR/agentcache
LOCKFILE=$CACHEFILE.lock
SEENFILE=$CACHEFILE.seenby.$REMOTE_HOST

# Check if we need to refresh the cache file. This needs
# to be done in mutual exclusion in order to avoid a clash
# with other monitoring servers calling us in parallel. This
# is not very unlikely since the agent might run a couple
# of seconds
(
    flock --timeout 180 --exclusive 200 || exit 1

    if [ ! -e "$CACHEFILE" ] || [ -e "$SEENFILE" ]; then
        check_mk_agent >$CACHEFILE
        rm -f $CACHEFILE.seenby.*
    fi
    # Send cache file and mark it as seen by our remote host
    cat $CACHEFILE
    touch "$SEENFILE"
) 200>$LOCKFILE
